package controller;

import DataAcess.Bus;
import Interface.MainGui;
import java.util.ArrayList;


/*
 * Class ID	: SE715
 * Student ID	: 02967
 * Student Name : Vu Ngoc Trung
 * Due Date     : Jul 12, 2013
 * I declare that this assignment is my own work in accordance with FPT Policy.
 */
public class ListAllController implements Controller {

    //Access List all form and array list contant all bus object
    private MainGui form;
    ArrayList<Bus> arrbus;
    //access controller that relationship with MainController
    controller.Controller controller;
    int mouseSelectedIndex;

    public ListAllController(ArrayList arrbus, controller.Controller controller,MainGui mgui) {
        //tranform array contain all bus object into this class array list for using in sub funtion
        this.arrbus = arrbus;
        this.controller = controller;
        form = mgui;
        mouseSelectedIndex = 0;

    }

    @Override
    public void handleEvent(String event) {

        if (event.contains("ListAll")) {
            ShowBang();
        }
        if (event.equals("View")) {
            ViewBus();
        }
        if (event.equals("Previous")) {
            previous();
        }
        if (event.equals("Next")) {
            Next();
        }
    }

    private void ShowBang() {

        int i = 0;
        for (Bus bus : arrbus) {
            form.tbDisplayAll.setValueAt(bus.getNumber(), i, 0);
            form.tbDisplayAll.setValueAt(bus.getName(), i, 1);
            i++;
        }
    }

    public void view(Bus bus) {
        form.lbnameDisplayAll.setText(bus.getName());
        form.lbNoDisplayAll.setText(Integer.toString(bus.getNumber()));
        form.taGoDisplayAll.setText(bus.getGo());
        form.taBackDisplayAll.setText(bus.getBack());
        form.taGoDisplayAll.setEditable(false);
        form.taBackDisplayAll.setEditable(false);
    }

    public void ViewBus() {
        if (form.tbDisplayAll.getValueAt(form.tbDisplayAll.getSelectedRow(), 0) != null) {
            mouseSelectedIndex = form.tbDisplayAll.getSelectedRow();
            view((Bus) arrbus.get(mouseSelectedIndex));
        }
    }

    public void previous() {
        if (mouseSelectedIndex > 0) {
            mouseSelectedIndex--;
        }
        form.tbDisplayAll.setRowSelectionInterval(mouseSelectedIndex, mouseSelectedIndex);
        view((Bus) arrbus.get(mouseSelectedIndex));
    }

    public void Next() {
        if (mouseSelectedIndex < arrbus.size()) {
            mouseSelectedIndex++;
        }
        view((Bus) arrbus.get(mouseSelectedIndex));
        form.tbDisplayAll.setRowSelectionInterval(mouseSelectedIndex, mouseSelectedIndex);
    }
}